package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUserVO;
import com.ruoyi.system.domain.SysRoleMenu;
import com.ruoyi.system.domain.dto.SysUserDTO;
import com.ruoyi.system.mapper.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.service.ISysUserService;

/**
 * 用户 业务层处理
 *
 * @author ruoyi
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper,SysUser> implements ISysUserService {
    @Autowired
    private SysUserRoleMapper userRoleMapper;
    @Autowired
    private SysRoleMapper roleMapper;

    @Override
    public void add(SysUserDTO params) {
        //检查手机号是否存在
        SysUser user = this.getOne(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser :: getPhone, params.getPhone())
                .eq(SysUser::getDelFlag,0));
        if(Objects.nonNull(user)){
            throw new CustomException("新增账号失败，手机号已存在");
        }
        SysUser entity = new SysUser();
        BeanUtils.copyProperties(params,entity);
        entity.setCreateBy(SecurityUtils.getLoginUser().getUser().getId());
        entity.setUpdateBy(SecurityUtils.getLoginUser().getUser().getId());
        entity.setPassword(SecurityUtils.encryptPassword(params.getPassword()));
        this.save(entity);
        List<SysUserRole> userRoleList = params.getRoleList().stream().map(item -> {
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(entity.getId());
            userRole.setRoleId(item);
            return userRole;
        }).collect(Collectors.toList());
        userRoleMapper.batchUserRole(userRoleList);
    }

    public static void main(String[] args) {
        System.out.println(SecurityUtils.encryptPassword("ztsyj888888"));
    }

    @Override
    public void updateUser(SysUserDTO.SysUserUpdate params) {
        SysUser entity = new SysUser();
        BeanUtils.copyProperties(params,entity);
        entity.setUpdateBy(SecurityUtils.getLoginUser().getUser().getId());
        this.saveOrUpdate(entity);
        //删除用户角色重新添加
        userRoleMapper.deleteUserRoleByUserId(entity.getId());
        List<SysUserRole> userRoleList = params.getRoleList().stream().map(item -> {
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(entity.getId());
            userRole.setRoleId(item);
            return userRole;
        }).collect(Collectors.toList());
        userRoleMapper.batchUserRole(userRoleList);
    }

    @Override
    public List<SysUserVO> listPage(SysUserDTO params) {
        List<SysUserVO> userList = this.baseMapper.getUserList(params);
        userList.forEach(item -> {
            item.setRoles(roleMapper.getRoleByUser(item.getId()));
        });
        return userList;
    }
}
